const { ipcMain, screen, BrowserWindow } = require('electron');
const path = require('path')
const url = require('url');
const log = require('electron-log');

const { OPEN_IMG_WINDOW, CLOSE_IMG_WINDOW, END_IMG_WINDOW, IMG_WINDOW_CLOSE_REPLY, SDK_APP_ID } = require("./const/const");


const getSrceenSize = () => {
    const display = screen.getPrimaryDisplay();
    return display.size;
}

class CallWindowIpc {
    callWindow = null;
    imWindow = global.WIN;
    readyToShowWindow = false;

    constructor() {
        this.mount();
    }

    mount() {
        this.createWindow();
        this.addEventListiner();
    }

    destroy() {
        this.offEventListiner();
        this.addEventListiner();
    }
//创建新窗口 windows
    createWindow() {
        this.callWindow = null;
        const { NODE_ENV, HUARUN_ENV } = process.env;
        const isDev = NODE_ENV?.trim() === 'development';
        const callWindow = new BrowserWindow({
            height: 640,
            width: 960,
            minWidth: 850,
            minHeight: 600,
            show: false,
            frame: false,
            webPreferences: {
                parent: this.win,
                webSecurity: true,
                nodeIntegration: true,
                nodeIntegrationInWorker: true,
                enableRemoteModule: true,
                contextIsolation: false,
            },
        });
        callWindow.removeMenu();
        if (isDev) {
            callWindow.loadURL(`http://localhost:3000/image.html`);
        } else {
            //callWindow.webContents.openDevTools(); //正式生产不需要开启
            callWindow.loadURL(
                url.format({
                    pathname: path.join(__dirname, `../../bundle/image.html`),
                    protocol: 'file:',
                    slashes: true
                })
            );
        }

        callWindow.on('ready-to-show', () => {
            this.readyToShowWindow = true;
        });

        this.readyToShowWindow = false;

        this.callWindow = callWindow;
    }

    addEventListiner() {
        const { NODE_ENV } = process.env;
        const isDev = NODE_ENV?.trim() === 'development';
        const screenSize = getSrceenSize();



        // 当接受方拒绝通话后，调用该方法可关闭窗口，并退出房间
        ipcMain.on(CLOSE_IMG_WINDOW, () => {
            this.callWindow.webContents.send('exit-room');
        });

        ipcMain.on(END_IMG_WINDOW, () => {
            this.callWindow.close()
        })


        ipcMain.on(OPEN_IMG_WINDOW, (event, data) => {

            if (data.windowType === 'imageWindow') {
                this.callWindow.setMinimumSize(400, 650);
                // this.callWindow.setSize(1000, 650);
                this.callWindow.setPosition(Math.floor((screenSize.width - 400) / 2), Math.floor((screenSize.height - 650) / 2));

            } else if (data.windowType === 'notificationWindow') {
                this.callWindow.setMinimumSize(320, 150);
                this.callWindow.setSize(320, 150);
                this.callWindow.setPosition(screenSize.width - 340, screenSize.height - 200);
                this.callWindow.setAlwaysOnTop(true);
            } else if (convType === 1 && Number(callType) === 1) {
                this.callWindow.setMinimumSize(400, 650);
                this.callWindow.setSize(400, 650);
                this.callWindow.setPosition(Math.floor((screenSize.width - 400) / 2), Math.floor((screenSize.height - 650) / 2));
            }

            if (data.windowType === 'meetingWindow') {
                this.callWindow.setMinimumSize(1240, 640);
                this.callWindow.setSize(1240, 640);
                this.callWindow.setResizable(true);
                this.callWindow.setPosition(Math.floor((screenSize.width - 1240) / 2), Math.floor((screenSize.height - 640) / 2));
            }

            const showWindow = (timer) => {
                this.callWindow.show();
                this.callWindow.webContents.send('pass-call-data', JSON.stringify(data));
                isDev && this.callWindow.webContents.openDevTools();
                timer && clearInterval(timer);

            }

            if (this.readyToShowWindow) {
                showWindow();
            } else {
                const timer = setInterval(() => {
                    if (this.readyToShowWindow) {
                        showWindow(timer);
                    }
                }, 10);

            }
        });

        this.callWindow.on('close', () => {
            try {
                this.createWindow()
            } catch (err) {
                console.log(err);
            }
        });

        this.callWindow.on('closed', () => {
            try {
                this.imWindow?.webContents.send(IMG_WINDOW_CLOSE_REPLY);
                this.destroy();
            } catch (err) {
                console.log(err);
            }
        });
    }

    offEventListiner() {
        ipcMain.removeAllListeners(OPEN_IMG_WINDOW);

        // 当接受方拒绝通话后，调用该方法可关闭窗口，并退出房间
        ipcMain.removeAllListeners(CLOSE_IMG_WINDOW);

        ipcMain.removeAllListeners(END_IMG_WINDOW)
    }
};

module.exports = CallWindowIpc;